package xyz.xpman.prisonerfitness.util;

import android.os.Build;
import android.util.Xml;

import androidx.annotation.RequiresApi;

import org.jetbrains.annotations.NotNull;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@SuppressWarnings("unused")
public class XmlUtil {
    public static final String UNIQUE_FLAG = "id";
    private static final String ROOT_SPACE_TAG = "data";

    private XmlUtil () {}


    public static Map<String, String> parseToMap(@NotNull String in) {
        XmlToMapHandler xmlHandler = new XmlToMapHandler();

        try {
            Xml.parse(in, xmlHandler);
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return xmlHandler.getData();
    }


    public static Map<String, String> parseToMap(@NotNull InputStream in) {
        XmlToMapHandler xmlHandler = new XmlToMapHandler();

        try {
            Xml.parse(in, Xml.Encoding.UTF_8, xmlHandler);
        } catch (SAXException | IOException e) {
            e.printStackTrace();
        }
        return xmlHandler.getData();
    }


    public static Map<String, String> parseToMap(@NotNull Reader in) {
        XmlToMapHandler xmlHandler = new XmlToMapHandler();

        try {
            Xml.parse(in, xmlHandler);
        } catch (SAXException | IOException e) {
            e.printStackTrace();
        }
        return xmlHandler.getData();
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String toXmlString(@NotNull Map<String, String> data){
        // 实现xml信息序列号的一个对象
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();

        try {
            // xml数据经过序列化后保存到String中，然后将字串通过OutputStream保存为xml文件
            serializer.setOutput(writer);

            // 文档开始
            serializer.startDocument(StandardCharsets.UTF_8.name(), true);

            final String rootSpace = "";

            // 开始一个子节点
            serializer.startTag(rootSpace ,ROOT_SPACE_TAG);
            serializer.attribute(rootSpace ,UNIQUE_FLAG,
                    data.getOrDefault(UNIQUE_FLAG, ""));


            for (Map.Entry<String, String> entry : data.entrySet()) {
                final String key = entry.getKey();

                serializer.startTag(rootSpace , key);
                serializer.text(entry.getValue());
                serializer.endTag(rootSpace , key);
            }

            serializer.endTag(rootSpace ,ROOT_SPACE_TAG);

            // 关闭文档
            serializer.endDocument();

        } catch (IllegalArgumentException | IOException | IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return writer.toString();
    }

}

